<?php

class UserDB {

    public static function getAllUsers() {
        try {
            $query = "SELECT * FROM user";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            if ($rowsAffected > 0) {
                $result = $stmt->fetchAll(PDO::FETCH_OBJ);
                $inventory = new Inventory();
                foreach ($result as $item) {
                    $user = self::getUserFromPDO($item);
                    $inventory->attach($user, $user->getUser_id());
                }
                return $inventory;
            } else {
                return null;
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function getUserByID($user_id) {//works
        try {
            $query = "SELECT * FROM user WHERE user_id = :user_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':user_id', $user_id);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                $result = $stmt->fetch(PDO::FETCH_OBJ);
                return self::getUserFromPDO($result);
            } else {
                return null; //user does not exists
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function getUserByEmail($email) {//works
        try {
            $query = "SELECT * FROM user WHERE email = :email";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':email', $email);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                $result = $stmt->fetch(PDO::FETCH_OBJ);
                return self::getUserFromPDO($result);
            } else {
                return null; //user does not exists
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function updateUser(User $user) {
        try {
            $query = "UPDATE user SET 
                name = :name, 
                address = :address,
                email = :email, 
                phone = :phone,
                address = :address WHERE email = :email";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);

            $name = $user->getName();
            $email = $user->getEmail();
            $phone = $user->getPhone();
            $address = $user->getAddress();

            $stmt->bindParam(':name', $name);
            $stmt->bindParam(':email', $email);
            $stmt->bindParam(':phone', $phone);
            $stmt->bindParam(':address', $address);

            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            return $rowsAffected;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function deleteUser($email) {//works
        try {
            $query = "DELETE FROM user WHERE email = :email";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':email', $email);
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            return $rowsAffected;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function insertUser(\User $user) {//works
        try {
            $query = "INSERT INTO user SET 
                name = :name,
                email = :email, 
                phone = :phone,
                address = :address";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);

            $name = $user->getName();
            $email = $user->getEmail();
            $phone = $user->getPhone();
            $address = $user->getAddress();

            $stmt->bindParam(':name', $name);
            $stmt->bindParam(':email', $email);
            $stmt->bindParam(':phone', $phone);
            $stmt->bindParam(':address', $address);

            $stmt->execute();

            $rowsAffected = $stmt->rowCount();
            return $rowsAffected;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    private static function getUserFromPDO($result) {//works
        $user = new \User();
        $user->setUser_id($result->user_id);
        $user->setEmail($result->email);
        $user->setName($result->name);
        $user->setPhone($result->phone);
        $user->setAddress($result->address);
        return $user;
    }

    public static function userExists($email) {//works
        try {
            $query = "SELECT * FROM user WHERE email = :email";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':email', $email);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                return true;
            } else {
                return false; //user does not exists
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }
    
     public static function getUserIDByEmail($email) {//works
        try {
            $query = "SELECT user_id FROM user WHERE email = :email";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':email', $email);
            $stmt->execute();
            $result = $stmt->fetchColumn();
            if ($result != FALSE) {
                return $result;
            } else {
                return false; //user does not exists
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }


}

?>
